define(function(require, exports, module) {
	var host_start = 'http://lh.sl.me:8080';
	require('jquery');
	require('fancybox');
	require('jscrollpane');
	require('mousewheel');
	require('jcookie');
	var user = require('user');

	function resumeModel(){
		this.init();
	};
	resumeModel.prototype = {
		base: {
			skin: { tit: '个人资料', dis: '请完善您的个人信息，<br />他们会自动同步到您的简历上。', submit: '确定', more: false,href: 'resume_index.html'},
			layout: [{type:'text',label:'中文名字',rule:$.testChinaName,postName:'cnname'},{type:'text',label:'英文名字',rule:$.testEnglish,postName:'enname'},{type:'radio',label:'性别',radiolabel:{'male':'男','female':'女'},def:'male',rule:$.testBasic,postName:'sex'},{type:'text',label:'户籍',rule:$.testChinese,postName:'housereg'},{type:'text',label:'出生日期',def:'1970/01/01',rule:$.testDate,postName:'birthday'},{type:'text',label:'手机号码',rule:$.testMobile,postName:'phone'},{type:'text',label:'邮箱地址',rule:$.testMail,postName:'email'}],
			json: {}
		},
		educate: {
			skin: { postName:'educationinfo', tit: '教育信息', tips: '<p>这一板块HR给你的时间只有10秒…20秒不能更多！</p><p>所以少年少女们，简单写明学校和主修专业。校园活动挑高大上的写，活动名称、担任职务、活动效果这三样就够了。</p><p>像多次获得廉师傅绿茶再来一瓶奖励这种就真不用写了。</p>',submit: '确定',more: true},
			layout: [{type:'text',label:'毕业院校',rule:$.testBasic},{type:'text',label:'学历/学位',rule:$.testBasic},{type:'text',label:'系/专业',rule:$.testBasic},{type:'text',label:'毕业日期',def:'2015/01/01',rule:$.testDate},{type:'textarea',label:'专业介绍',sw:'size1of1',rule:$.testBasic}],
			json: {}
		},
		work: {
			skin: { postName:'workhistory', tit: '工作经验', tips: '<p>你来到了HR一定会重点关注的板块。表紧张，按照下面的三段式来写：</p><p>公司名称<br />担任岗位<br />主要职责<br />三段句式:“负责了…”“参与了…”“获得了…”</p><p>就是这样So easy. 妈妈再也不用担心你写简历。</p><p>举例：<br />2010.4-2010.11<br />赫斯特广告有限公司上海分公司</p><p>订阅部实习生</p><p>主要负责时尚杂志天猫店首页网站的美化和修改工作，包括轮播图 、banner图等。另外还负责制作宣传展板，宣传DM单以及易拉宝等宣传制品，得到领导和同事的一致好评</p>',submit: '确定',more: true},
			layout: [{type:'text',label:'公司名称',rule:$.testBasic},{type:'text',label:'部门/职位',rule:$.testBasic},{type:'text',label:'入职时间',def:'2015/01/01',rule:$.testDate},{type:'text',label:'离职时间',def:'2015/01/01',rule:$.testDate},{type:'textarea',label:'工作职责',sw:'size1of1',rule:$.testBasic}],
			json: {}
		},
		valuate: {
			skin: { tit: '个人评价', tips: '<p>想想你是怎么去追男神女神的，投其所好才是王道！</p><p>个人评价也是一样，针对不同岗位的要求凸显自己不同的特质，但是千万不要编造噢，因为HR的火眼金睛可是会马上识破的噢。</p><p>像市场、销售岗位更偏好性格外向的人；IT技术岗位更偏向逻辑清晰，动手能力强的人；银行、金融岗位更偏好对数字敏感的人……</p><p>如果还不知道该怎么写，你知道这世界上有一种叫valuate职业测试的东西吗？</p><p>去做个测试，你的职场性格立马呈现在眼前。</p>',submit: '确定',testdis:'倘若您不知道该如何撰写自我评价，也可以利用我们的职<br />业测评工具帮助发现您的个人特质，从而给HR提供参考。', more: false},
			layout: [{type:'textarea',rule:$.testBasic,sw:'size1of0',postName:'selfevaluation'}],
			json: {}
		},
		tags: {
			skin: { tit: '个性标签', tips: '<p>恭喜你，终于来到了这个有趣又好玩的板块。这里有海量丰富的ICON供你选择。</p><p>点击最能代表你自己的标签，立刻展现在简历上。马上让你的简历创意十足，立刻和那些山炮土肥圆简历拉开差距！</p>',submit: '确定', more: false},
			layout: [{type:'tag',sw:'size1of0',arr:[{name:'兴趣',tags:['运动','看书','旅游','唱歌','音乐','街拍','写作','资深宅','小清新','数码控','自拍狂魔','女汉子','真直男','萌妹子','天然呆','二次元','八卦','吃货','发呆','极客','游戏达人']},{name:'设计',tags:['PS','Ai','Id','Fl','Ae','Dw','Pr','Lr','AutoCAD','3DMax','Cinema4D','Maya']},{name:'技术',tags:['Html','Java','JavaScript','C#','C++','MySQL','SQLServer','.Net','.Php','Node.js','Ruby','Oracle','VB','Python']}],postName:'selftags'}],
			json: {}
		},
		other: {
			skin: { tips: '<p></p>',submit: '确定', more: false},
			layout: [{type:'textarea',sw:'size1of0',postName:'supplement'}],
			json: {}
		},
		creatTips: function(html,skin,tit){
			var tip_pop = $('<div class="tip_pop"><h2>小贴士</h2><div class="context">'+skin.tips+'</div></div>').insertBefore(tit);
			var tip_toggle = $('<div class="inline_block tip_toggle"><span class="icon icon_tips"></span><span class="icon icon_tips_flw"></span></div>').appendTo(tit);
			$(tip_toggle)[0].bl = false;
			var fancy_block = $(html).find('.basicForm'),
				w = fancy_block.width(),
				ww = tip_pop.width();
			tip_toggle.on('click',function(){
				if($(this)[0].bl){
					$(this)[0].bl =  false;
					$(this).removeClass('active');
					fancy_block.width(w);
					tip_pop.hide();
					$.fancybox.toggle(1000);
				}else{
					$(this)[0].bl =  true;
					$(this).addClass('active');
					fancy_block.width(w+ww);
					tip_pop.show();
					$.fancybox.toggle(1000);
				};
			});
		},
		creatTags: function(dot,_data,form_in){
			var dw = 71;
			var dot
			var arr = dot['arr'];
			var tag_nav = $('<div class="tag_nav cf"></div>');
			var tag_cont = $('<div class="tag_cont cf"></div>');
			var collect_culm = $('<div class="collect_culm cf"><div class="dis">您最多选择<span>9</span>个您的个性标签</div><div class="culm culm_tag tag_cont cf"></div></div>');
			for(var ii=0;ii<arr.length;ii++){
				var d = arr[ii];
				var dr = arr[ii]['tags'];
				var tag_nav_item = $('<div class="item tag_nav_item">'+d['name']+'</div>');
				var tag_cont_item = $('<div class="item tag_cont_item"></div>');
				tag_nav_item.appendTo(tag_nav);
				tag_cont_item.appendTo(tag_cont);
				for(var oo=0;oo<dr.length;oo++){
					var icon = $('<div title="'+dr[oo]+'" style="background-position: -'+oo*dw+'px -'+ii*dw+'px" class="dot dot_'+ii+'_'+oo+'" data-dotxt="'+dr[oo]+'"></div>').appendTo(tag_cont_item);
					icon.on('click',function(){
						var icon_sf = this;
						if(!$(this).hasClass('checked') && collect_culm.find('.dot').length<9){
							$(this).addClass('checked');
							var cc = $(this).clone();
							cc.on('click',function(){
								$(icon_sf).removeClass('checked');
								$(this).remove();
								collect_culm.find('.dis span').text(9-collect_culm.find('.dot').length);
							});
							cc.appendTo(collect_culm.find('.tag_cont'));
							collect_culm.find('.dis span').text(9-collect_culm.find('.dot').length);
						}else{

						}
					});
					var data_arr = _data[0].split(',');
					if(arr.length>0 && $.inArray(dr[oo], data_arr)>=0){
						icon.click();
					};
				};
				if(ii==0){
					tag_nav_item.addClass('active');
					tag_cont_item.addClass('active');
				};
			};
			tag_nav.appendTo(form_in);
			tag_cont.appendTo(form_in);
			collect_culm.appendTo(form_in);
			tag_nav.find('.item').on('click',function(){
				$(this).addClass('active').siblings().removeClass('active');
				tag_cont.find('.item').eq($(this).index()).addClass('active').siblings().removeClass('active');
			});
		},
		creatRadio: function(rl,form_in,str,creatLiIndex,i,val){
			for(var o in rl){
				var radio_item = $('<div class="left radio_item"><label class="cos_radio" for="'+(str+'_'+creatLiIndex+'_'+i+'_'+o)+'"><div class="cos_inner"><input id="'+(str+'_'+creatLiIndex+'_'+i+'_'+o)+'" name="'+(str+creatLiIndex+i)+'" value="'+rl[o]+'" type="radio" class="form_radio" /></div></label><span class="flw left">'+rl[o]+'</span></div>');
				//console.log(val)
				var n = val ? val : dot['def'];
				if(n == rl[o]){
					radio_item.addClass('checked');
					radio_item.find('input').attr('checked', true);
				}else{
					radio_item.find('input').attr('checked', false);
				}
				radio_item.appendTo(form_in);
			};
			form_in.find('.radio_item').each(function(index,item){
				$(item).find('input').on('change',function(e){
					if(e.currentTarget.checked){
						$(item).addClass('checked').siblings().removeClass('checked');
					};
				})
			});
		},
		creatHTML: function(_str,_nextStr){
			var sf = this,
				creatLiIndex = 0,
				str = _str ? _str : 'base',
				nextStr = _nextStr ? _nextStr : '',
				skin = this[str]['skin'],
				layout  = this[str]['layout'],
				data = this[str]['json'];
			var html = $('<div class="basic_info_wrap"><div class="basicForm '+str+'Form"><div class="basicFormInner '+str+'FormInner"><div class="form_wrap"><form name="form_'+str+'" id="form_'+str+'"><div class="form_list_scroll"><div class="form_list_scroll_inner"></div></div><div class="submit_bar"><input class="form_submit" name="'+str+'Submit" type="button" value="'+skin.submit+'" /></div></form></div></div></div></div>');
			var tit = $('<h2 class="tit txtC"><span class="txt">'+skin.tit+'</span></h2>').insertBefore(html.find('.form_wrap'));
			var form_list_scroll_inner = html.find('.form_list_scroll_inner');
			if(skin.dis){
				$('<div class="dis txtC">'+skin.dis+'</div>').insertAfter(tit);
			};
			if(skin.tips){
				sf.creatTips(html,skin,tit);
			};
			function creatLi(_data){
				creatLiIndex++;
				var list_wrap = $('<div class="form_list_wrap"></div>');
				var list = $('<ul class="form_list cf"></ul>');
				var tit = $('<div class="form_tit cf"><h3 class="left">'+skin.tit+creatLiIndex+'</h3></div>');
				if(skin.more){
					tit.appendTo(list_wrap);
					var del_btn = $('<span class="right icon_remove btn_remove">删除</span>').appendTo(tit);
					del_btn.on('click',function(){
						creatLiIndex--;
						if(creatLiIndex<1){
							creatLiIndex =1;
							return false;
						}
						list_wrap.remove();
						//console.log(add_bar,creatLiIndex)
						if(creatLiIndex>=3){
							add_bar.addClass('disable');
						}else{
							add_bar.removeClass('disable');
						}
						html.find('.form_list_scroll').jScrollPane();
					});
				};
				if(skin['postName']){
					list.data('post',skin['postName']).addClass('post_arr');
				};
				for(var i=0;i<layout.length;i++){
					var form_in;
					var dot = layout[i];
					var val = _data && _data[i] ? _data[i] : '';
					var def = dot['def'] ? dot['def'] : '';
					if(dot['type']=='tag'){
						form_in = $('<div class="tag_form_in"></div>');
						sf.creatTags(dot,_data,form_in);
					};
					if(dot['type'] == 'text'){
						form_in = $('<input type="text" id="'+str+'_'+creatLiIndex+'_'+i+'" name="'+str+'_'+creatLiIndex+'_'+i+'" value="'+val+'" class="form_culm form_text" placeholder="'+def+'" />');
					};
					if(dot['type'] == 'radio'){
						var rl = dot['radiolabel'];
						form_in = $('<div></div>');
						sf.creatRadio(rl,form_in,str,creatLiIndex,i,val);
					};
					if(dot['type'] == 'textarea'){
						form_in = $('<textarea id="'+str+'_'+creatLiIndex+'_'+i+'" name="'+str+'_'+creatLiIndex+'_'+i+'" class="form_culm form_textarea">'+val+'</textarea>');
					};
					var sw = dot['sw'] ? dot['sw'] : 'size1of2';
					var li = $('<li  data-index="'+i+'" class="'+sw+'"><div class="input_text_wrap left"></div></li>');
					li.data('post',dot['postName']);
					
					if(dot['label']){
						$('<label class="left">'+dot['label']+'</label>').prependTo(li);
					}
					form_in.appendTo(li.find('.input_text_wrap'));
					li.appendTo(list);
				};
				list.appendTo(list_wrap);
				list_wrap.appendTo(form_list_scroll_inner);
			};
			if(skin.more){
				var add_bar = $('<div class="add_bar cf"><span class="right icon_add add_btn">添加'+skin.tit+'</span></div>').insertAfter(html.find('.form_list_scroll'));
				add_bar.find('.add_btn').on('click',function(){
					if(creatLiIndex>=3){
						return false;
					};
					creatLi();
					if(creatLiIndex>=3){
						add_bar.addClass('disable');
					}else{
						add_bar.removeClass('disable');
					};
					html.find('.form_list_scroll').jScrollPane();
				});
				setTimeout(function(){
					html.find('.form_list_scroll').jScrollPane();
				},100);
			};
			if(skin.testdis){
				$('<div class="bar_test_mbti cf"><span class="txt">'+skin.testdis+'</span><div class="btn_test_mbti"></div></div>').appendTo(form_list_scroll_inner);
			}
			$(html).find('.form_submit').on('click',formFn);
			function formFn(e){
				var form = html.find('form'),
					postData = {},
					passBl = true;
				$(form).find('ul.form_list').each(function(uind,uitm){
					var ul_post = $(this).data('post');
					var ul_arr = null;
					if($(uitm).hasClass('post_arr')){
						ul_arr = [];
					};
					$(uitm).find('li').each(function(index,item){
						var i = $(item).data('index'),
							li_post = $(item).data('post');
						var rule = layout[i]['rule'];
						var val = '';
						if(ul_arr){
							val = $.trim($(item).find('.form_culm').val());
							ul_arr.push($.trim($(item).find('.form_culm').val()));
							
						}else{
							if($(item).find('input:radio').length){
								val = $(item).find('input:checked').val();
							}else if($(item).find('textarea').length){
								val = $.trim($(item).find('textarea').val());
							}else if($(item).find('.culm_tag').length){
								$('.culm_tag .dot').each(function(dot_ind, dot_el) {
									val += $(dot_el).data('dotxt')+',';
								});
							}else{
								val = $.trim($(item).find('input').val());
							};
							postData[li_post] = val;
						};
						var tests = true;
						if(rule){
							tests = rule(val);
						};
						if(val===''){
							tests = false;
						};
						if(!tests){
							passBl = false;
							$(item).addClass('error');
						}else{
							$(item).removeClass('error');
						};
					});
					if(ul_arr){
						ul_arr.push(1);
						ul_arr.push(1);
						ul_arr.push(1);
						ul_arr.push(1);
						ul_arr.push(1);
						postData[ul_post+(uind+1)] = ul_arr;
					};
				});
				if(passBl){
					////console.log('post',postData);
					data = postData;
					console.log(postData)
					$.ajax({
						url: host_start + '/resume/create/?act='+str,
						type:'POST',
						data: postData,
						dataType: 'json',
						success: function(data){
							//console.log(data)
						}
					}); 
					if(skin.href){
					 	//window.location.href = skin.href;
					}else{
						
						//sf.model_refresh(str);
						//$.fancybox.close();
					};
				}else{
					//console.log('false',postData);
				}
			};
			if(data.length){
				for(var ind=0;ind<data.length;ind++){
					creatLi(data[ind]);
				};
			}else{
				creatLi();
			};
			return html;
		},
		model_refresh: function(e){
			////console.log(e,this['json'][e])
		},
		show_other: function(_obj){
			var sf= this;
			var str = 'other';
			var nextStr = '';
			var html = this.creatHTML(str,nextStr);
			$.fancybox.open(html,{
				wrapCSS : 'own_skin form_skin',
				padding : 0
			});
		},
		show_tags: function(_obj){
			var sf= this;
			var str = 'tags';
			var nextStr = '';
			var html = this.creatHTML(str,nextStr);
			$.fancybox.open(html,{
				wrapCSS : 'own_skin form_skin',
				padding : 0
			});
		},
		show_valuate: function(_obj){
			var sf= this;
			var str = 'valuate';
			if(_obj && _obj.nextBl){
				var nextStr = '';
			}else{
				var nextStr = 'tags';
			};
			var html = this.creatHTML(str,nextStr);
			$.fancybox.open(html,{
				wrapCSS : 'own_skin form_skin',
				padding : 0
			});
		},
		show_work: function(_obj){
			var sf= this;
			var str = 'work';
			if(_obj && _obj.nextBl){
				var nextStr = '';
			}else{
				var nextStr = 'valuate';
			};
			var html = this.creatHTML(str,nextStr);
			$.fancybox.open(html,{
				wrapCSS : 'own_skin form_skin',
				padding : 0
			});
		},
		show_educate: function(_obj){
			var sf= this;
			var str = 'educate';
			if(_obj && _obj.nextBl){
				var nextStr = '';
			}else{
				var nextStr = 'work';
			};
			var html = this.creatHTML(str,nextStr);
			$.fancybox.open(html,{
				wrapCSS : 'own_skin form_skin',
				padding : 0
			});
		},
		show_base: function(_obj){
			var sf= this;
			var str = 'base';
			if(_obj && _obj.nextBl){
				var nextStr = '';
			}else{
				var nextStr = 'educate';
			};
			var html = this.creatHTML(str,nextStr);
			$.fancybox.open(html,{
				wrapCSS : 'own_skin form_skin',
				padding : 0
			});
		},
		init: function(){
			
		}
	};
	$(document).ready(function(){
		var resume_model = new resumeModel();
		$.ajax({
			url: host_start + '/resume/view',
			type:'GET',
			dataType: 'json',
			success: function(d){
				for(var i in d){
					console.log(d[i],i)
				};
				var msg = {
					base : [['胡子','Holy','男','湖南','1983/11/11','13465575736','ad@ad.com']],//base : {birthday:'1983/11/11','cnname':'胡子','email:'ad@ad.com','enname:'Holy','housereg:'湖南','phone:'13465575736','sex:'男'},
					educate : [['冲突','本科','中文','2015/01/01','discription'],['冲突','本科','中文','2015/01/01','discription'],['冲突','本科','中文','2015/01/01','discription']],
					work : [['冲突','本科','2015/01/01','2015/01/01','discriptions'],['冲突','本科','2015/01/01','2015/01/01','discriptions'],['冲突','本科','2015/01/01','2015/01/01','discriptions']],
					valuate : [['冲突']],
					tags : [["游戏达人,极客,真直男,JavaScript,Html"]]
				};
				for(var i in msg){
					resume_model[i]['json'] = msg[i];
					resume_model.model_refresh(i,msg[i])
				};
				$('.abcenter_btn').on('click',function(){
					resume_model.show_base();
						return false;
				});
				$('.resume_node_edit .info_item').each(function(index,item){
					var cover = $('<div class="cover"><div class="txt"><img src="assets/images/edit_txt.png" /></div></div>');
					$(item).hover(
						function(e){
							cover.appendTo(item);
						},
						function(e){
							cover.remove();
						}
					);
					$(item).on('dblclick',function(){
						resume_model['show_'+$(this).data('form')]();
					});
				});
			}
		});
	});
});